package com.bootcamp.s2_0922.ds._0923;

/**
 * @program: 35th_2_DS_snowlee
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-23 11:29
 **/

public class Demo1_MhyList {
    public static void main(String[] args) {
        MyList myList = new MyList();
        myList.add("a1");
        myList.add("a2");
        myList.add("a3");
        myList.add("a4");
        myList.add("a5");
        myList.add("a6");
        MyList myList1 = new MyList();
        myList1.remove("a1");
    }
}

class MyList {

    class Node {
        String value;
        Node next;

        public Node(String value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    private Node head; // Mylist head node
    private int size; // store element number

    public MyList() {
    }

    /**
     * judge list is empty or not
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    public boolean add(String element) {
        if (isEmpty()) {
            // when list is empty
            head = new Node(element, null);
            size = 1;
            return true;
        }
        Node curr = head;
        while (curr.next != null) {
            curr = curr.next;
        }
        // when loop end, curr represent rear node
        curr.next = new Node(element, null);
        size++;
        return true;
    }

    public void add(int index, String element) {

    }

    public boolean remove(String element) {
        // if list is null, throw new exception
        if (isEmpty()) throw new RuntimeException("list is null!");
        // if delete null, using ==
        if (element == null) {
            // if delete head node
            if (head.value == element) {
                head = head.next;
                size--;
                return true;
            }
            // if not delete head node

        }
        // if string is not null, using equals
        return false;
    }


}